﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraNavBar;
using DevExpress.XtraBars.Helpers;
using DevExpress.XtraSplashScreen;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraBars.Ribbon.Gallery;
using DevExpress.XtraRichEdit;
using DevExpress.XtraGrid;
using DevExpress.XtraPrinting;
using DevExpress.Utils.About;
using SmartFrame_DataCollect.Forms;
using DevExpress.XtraEditors.ColorWheel;
using DevExpress.LookAndFeel;
using DevExpress.Utils.Taskbar.Core;
using DevExpress.Utils.Taskbar;
using System.Globalization;
using System.Threading;
using DevExpress.XtraScheduler.Native;

namespace SmartFrame_DataCollect
{
    public partial class frmMain : RibbonForm
    {
        MailType currentMailType = MailType.Inbox;
        public ModulesNavigator modulesNavigator;
        internal FilterColumnsManager FilterColumnManager;
        ZoomManager zoomManager;
        List<BarItem> AllowCustomizationMenuList = new List<BarItem>();
        frmLogin frm;

        public frmMain()
        {
            //SplashScreenManager.ShowForm(null, typeof(ssMain), true, true, false, 1000);

            //TaskbarHelper.InitDemoJumpList(TaskbarAssistant.Default, this);
            frm = new frmLogin(this);
            InitializeComponent();
            #region 自定义初始化控件
            //this.ucMailTree1 = new SmartFrame_DataCollect.ucMailTree(this);

            //this.navBarGroupControlContainer1.Controls.Add(this.ucMailTree1);
            //resources.ApplyResources(this.navBarGroupControlContainer1, "navBarGroupControlContainer1");
            this.navBarGroupControlContainer1.Name = "navBarGroupControlContainer1";
            // 
            // ucMailTree1
            // 
            //resources.ApplyResources(this.ucMailTree1, "ucMailTree1");
            //this.ucMailTree1.Name = "ucMailTree1";
            //this.ucMailTree1.DataSourceChanged += new SmartFrame_DataCollect.DataSourceChangedEventHandler(this.ucMailTree1_DataSourceChanged);
            //this.ucMailTree1.ShowMenu += new System.Windows.Forms.MouseEventHandler(this.ucMailTree1_ShowMenu);
            //this.ucMailTree1.UCTreeDragDrop += new SmartFrame_DataCollect.UCTreeDragDropEventHandler(this.ucMailTree1_UCTreeDragDrop);
            this.barButtonItem3.ItemClick += barButtonItem3_ItemClick;
            this.bbiMSSQL.ItemClick += DB_ItemClick;
            this.bbiRedis.ItemClick += DB_ItemClick;
            this.bbiMongoDB.ItemClick += DB_ItemClick;


            #endregion


            rpcSearch.Text = TagResources.SearchTools;
            InitNavBarGroups();
            SkinHelper.InitSkinGallery(rgbiSkins);
            RibbonButtonsInitialize();
            modulesNavigator = new ModulesNavigator(ribbonControl1, pcMain);
            zoomManager = new ZoomManager(ribbonControl1, modulesNavigator, beiZoom);
 
            NavigationInitialize();
            SetPageLayoutStyle();
            this.barSubItem4.Visibility = BarItemVisibility.Never;
            this.Enabled = false;
            frm.Show(this);

        }

        void NavigationInitialize()
        {
            foreach (NavBarGroup group in navBarControl1.Groups)
            {
                BarButtonItem item = new BarButtonItem(ribbonControl1.Manager, group.Caption);
                item.Tag = group;
                item.Glyph = group.SmallImage;
                item.ItemClick += new ItemClickEventHandler(item_ItemClick);
                bsiNavigation.ItemLinks.Add(item);
            }
        }

        public void changeModulesNavigator()
        {
            modulesNavigator.ChangeGroup(navBarControl1.ActiveGroup, this);
        }

        void item_ItemClick(object sender, ItemClickEventArgs e)
        {
            navBarControl1.ActiveGroup = (NavBarGroup)e.Item.Tag;
        }
        void RibbonButtonsInitialize()
        {
            //InitBarButtonItem(bbiRotateLayout, TagResources.RotateLayout, Properties.Resources.RotateLayoutDescription);
            //InitBarButtonItem(bbiFlipLayout, TagResources.FlipLayout, Properties.Resources.FlipLayoutDescription);
            InitBarButtonItem(bblLog, TagResources.DeleteItem, Properties.Resources.DeleteItemDescription);
            InitBarButtonItem(bbiNew, TagResources.NewMail, Properties.Resources.NewItemDescription);
            InitBarButtonItem(bbiReply, TagResources.Reply, Properties.Resources.ReplyDescription);
            InitBarButtonItem(bbiReplyAll, TagResources.ReplyAll, Properties.Resources.ReplyAllDescription);
            InitBarButtonItem(bbiForward, TagResources.Forward, Properties.Resources.ForwardDescription);
            InitBarButtonItem(bbiUnreadRead, TagResources.UnreadRead, Properties.Resources.UnreadReadDescription);
            InitBarButtonItem(bbiCloseSearch, TagResources.CloseSearch, Properties.Resources.CloseSearchDescription);
            InitBarButtonItem(bbiSubjectColumn, TagResources.SubjectColumn);
            InitBarButtonItem(bbiFromColumn, TagResources.PersonColumn);
            InitBarButtonItem(bbiDateColumn, TagResources.DateColumn);
            InitBarButtonItem(bbiPriorityColumn, TagResources.PriorityColumn);
            InitBarButtonItem(bbiAttachmentColumn, TagResources.AttachmentColumn);
            InitBarButtonItem(bbiResetColumnsToDefault, TagResources.ResetColumnsToDefault);
            InitBarButtonItem(bbiDate, TagResources.DateFilterMenu);
            InitBarButtonItem(bbiClearFilter, TagResources.ClearFilter);
            InitBarButtonItem(bbiNewFeed, TagResources.FeedNew, Properties.Resources.NewFeedDescription);
            InitBarButtonItem(bbiEditFeed, TagResources.FeedEdit, Properties.Resources.EditFeedDescription);
            InitBarButtonItem(bblLogFeed, TagResources.FeedDelete, Properties.Resources.DeleteFeedDescription);
            InitBarButtonItem(bbiRefreshFeed, TagResources.FeedRefresh, Properties.Resources.RefreshFeedDescription);
            InitBarButtonItem(bbiNewContact, TagResources.ContactNew, Properties.Resources.NewContactDescription);
            InitBarButtonItem(bbiEditContact, TagResources.ContactEdit, Properties.Resources.EditContactDescription);
            InitBarButtonItem(bblLogContact, TagResources.ContactDelete, Properties.Resources.DeleteContactDescription);
            InitBarButtonItem(bbiNewTask, TagResources.TaskNew, Properties.Resources.NewTaskDescription);
            InitBarButtonItem(bbiEditTask, TagResources.TaskEdit, Properties.Resources.EditTaskDescription);
            InitBarButtonItem(bblLogTask, TagResources.TaskDelete, Properties.Resources.DeleteTaskDescription);
            InitBarButtonItem(bbiTodayFlag, FlagStatus.Today, Properties.Resources.FlagTodayDescription);
            InitBarButtonItem(bbiTomorrowFlag, FlagStatus.Tomorrow, Properties.Resources.FlagTomorrowDescription);
            InitBarButtonItem(bbiThisWeekFlag, FlagStatus.ThisWeek, Properties.Resources.FlagThisWeekDescription);
            InitBarButtonItem(bbiNextWeekFlag, FlagStatus.NextWeek, Properties.Resources.FlagNextWeekDescription);
            InitBarButtonItem(bbiNoDateFlag, FlagStatus.NoDate, Properties.Resources.FlagNoDatekDescription);
            InitBarButtonItem(bbiCustomFlag, FlagStatus.Custom, Properties.Resources.FlagCustomDescription);
            //InitBarButtonItem(bbiShowPreview, TagResources.Preview, Properties.Resources.ShowPreviewDescription);
            InitGalleryItem(rgbiCurrentView.Gallery.Groups[0].Items[0], TagResources.ContactList, Properties.Resources.ContactListDescription);
            InitGalleryItem(rgbiCurrentView.Gallery.Groups[0].Items[1], TagResources.ContactAlphabetical, Properties.Resources.ContactAlphabeticalDescription);
            InitGalleryItem(rgbiCurrentView.Gallery.Groups[0].Items[2], TagResources.ContactByState, Properties.Resources.ContactByStateDescription);
            InitGalleryItem(rgbiCurrentView.Gallery.Groups[0].Items[3], TagResources.ContactCard, Properties.Resources.ContactCardDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[0], TagResources.TaskList, Properties.Resources.TaskListDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[1], TagResources.TaskToDoList, Properties.Resources.TaskToDoListDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[2], TagResources.TaskCompleted, Properties.Resources.TaskCompletedDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[3], TagResources.TaskToday, Properties.Resources.TaskTodayDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[4], TagResources.TaskPrioritized, Properties.Resources.TaskPrioritizedDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[5], TagResources.TaskOverdue, Properties.Resources.TaskOverdueDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[6], TagResources.TaskSimpleList, Properties.Resources.TaskSimpleListDescription);
            InitGalleryItem(rgbiCurrentViewTasks.Gallery.Groups[0].Items[7], TagResources.TaskDeferred, Properties.Resources.TaskDeferredDescription);
            //bvbiSaveAs.Tag = TagResources.MenuSaveAs;
            //bvbiSaveAttachment.Tag = TagResources.MenuSaveAttachment;
            //bvbiSaveCalendar.Tag = TagResources.MenuSaveCalendar;
            bbiPriority.Hint = Properties.Resources.PriorityDescription;
            bsiNavigation.Hint = Properties.Resources.NavigationDescription;
            bbiShowUnread.Hint = Properties.Resources.SearchUnreadDescription;
            bbiDate.Hint = Properties.Resources.SearchReceivedDescription;
            bbiImportant.Hint = Properties.Resources.SearchImportantDescription;
            bbiHasAttachment.Hint = Properties.Resources.SearchHasAttachmentDescription;
            bbiClearFilter.Hint = Properties.Resources.SearchClearDescription;
            bsiColumns.Hint = Properties.Resources.SearchColumnsDescription;
            bbiResetColumnsToDefault.Hint = Properties.Resources.SearchResetDescription;
            bbiCloseSearch.Hint = Properties.Resources.SearchCloseDescription;
            bbiReminder.Glyph = Properties.Resources.reminder;

            List<BarButtonItem> items = new List<BarButtonItem>();
            items.Add(bbiSubjectColumn);
            items.Add(bbiFromColumn);
            items.Add(bbiDateColumn);
            items.Add(bbiPriorityColumn);
            items.Add(bbiAttachmentColumn);
            items.Add(bbiDate);
            FilterColumnManager = new FilterColumnsManager(items);
            //ucContacts1.SynchronizeGalleryItems(rgbiCurrentView);
            //ucCalendar1.SetBarController(schedulerBarController1);
            //AllowCustomizationMenuList.Add(bbiFlipLayout);
            //AllowCustomizationMenuList.Add(bbiRotateLayout);
            AllowCustomizationMenuList.Add(bsiNavigation);
            AllowCustomizationMenuList.Add(rgbiSkins);
            ribbonControl1.Toolbar.ItemLinks.Add(rgbiSkins);
        }

        void InitGalleryItem(GalleryItem galleryItem, string tag, string description)
        {
            galleryItem.Tag = tag;
            galleryItem.Hint = description;
        }
        //internal bool ShowPreview { get { return bbiShowPreview.Down; } }
        internal ZoomManager ZoomManager { get { return zoomManager; } }
        internal BarButtonItem ShowUnreadItem { get { return bbiShowUnread; } }
        internal BarButtonItem ImportantItem { get { return bbiImportant; } }
        internal BarButtonItem HasAttachmentItem { get { return bbiHasAttachment; } }
        internal BarButtonItem ClearFilterItem { get { return bbiClearFilter; } }
        //internal BackstageViewButtonItem SaveAsMenuItem { get { return bvbiSaveAs; } }
        //internal BackstageViewButtonItem SaveAttachmentMenuItem { get { return bvbiSaveAttachment; } }
        //internal BackstageViewButtonItem SaveCalendar { get { return bvbiSaveCalendar; } }
        internal InRibbonGallery TaskGallery { get { return rgbiCurrentViewTasks.Gallery; } }
        internal PopupMenu FlagStatusMenu { get { return pmFlagStatus; } }
        void InitBarButtonItem(DevExpress.XtraBars.BarButtonItem buttonItem, object tag)
        {
            InitBarButtonItem(buttonItem, tag, string.Empty);
        }
        void InitBarButtonItem(DevExpress.XtraBars.BarButtonItem buttonItem, object tag, string description)
        {
            buttonItem.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(bbi_ItemClick);
            buttonItem.Hint = description;
            buttonItem.Tag = tag;
        }
        void InitNavBarGroups()
        {
            nbgMail.Tag = new NavBarGroupTagObject("DeviceDetails", typeof(SmartFrame_DataCollect.DeviceDetails));
            nbgCalendar.Tag = new NavBarGroupTagObject("AnalysisRpt", typeof(SmartFrame_DataCollect.AnalysisRpt));
            nbgContacts.Tag = new NavBarGroupTagObject("BIView", typeof(SmartFrame_DataCollect.BIView));
            nbgDataConfig.Tag = new NavBarGroupTagObject("DataConfigTab", typeof(SmartFrame_DataCollect.DataConfigTab));


            //nbgFeeds.Tag = new NavBarGroupTagObject("Feeds", typeof(SmartFrame_DataCollect.Feeds));
            //nbgTasks.Tag = new NavBarGroupTagObject("Tasks", typeof(SmartFrame_DataCollect.Tasks));
        }
        public void ReadMessagesChanged()
        {
            //ucMailTree1.RefreshTreeList();
        }
        public void UpdateTreeViewMessages()
        {
            //ucMailTree1.UpdateTreeViewMessages();
        }
        internal void EnableDelete(bool enabled)
        {
            bblLog.Enabled = enabled;
            bbiUnreadRead.Enabled = enabled;
            bbiPriority.Enabled = enabled;
        }
        internal void EnableMail(bool enabled, bool unread)
        {
            bbiReply.Enabled = enabled && currentMailType == MailType.Inbox;
            bbiReplyAll.Enabled = enabled && currentMailType == MailType.Inbox;
            bbiForward.Enabled = enabled && currentMailType == MailType.Inbox;
        }
        internal void EnableEditFeed(bool enabled)
        {   
            bblLogFeed.Enabled = enabled;
            bbiEditFeed.Enabled = enabled;
            bbiRefreshFeed.Enabled = enabled;
        }
        internal void EnableEditContact(bool enabled)
        {
            bblLogContact.Enabled = enabled;
            bbiEditContact.Enabled = enabled;
        }
        internal void EnabledFlagButtons(bool enabledCurrentTask, bool enabledEdit, Task task)
        {
            List<BarButtonItem> list = new List<BarButtonItem> { bbiTodayFlag, bbiTomorrowFlag, bbiThisWeekFlag,
                bbiNextWeekFlag, bbiNoDateFlag, bbiCustomFlag };
            foreach (BarButtonItem item in list)
            {
                item.Enabled = enabledCurrentTask;
                if (task != null)
                    item.Down = task.FlagStatus.Equals(item.Tag);
                else item.Down = false;
            }
            bblLogTask.Enabled = enabledCurrentTask;
            bbiEditTask.Enabled = enabledEdit;
        }
        internal void EnableZoomControl(bool enabled)
        {
            beiZoom.Enabled = enabled;
        }
        internal void SetPriorityMenu(PopupMenu menu)
        {
            bbiPriority.DropDownControl = menu;
        }
        internal void SetDateFilterMenu(PopupMenu menu)
        {
            bbiDate.DropDownControl = menu;
        }
        internal void ShowMessageMenu(Point location)
        {
            pmMessage.ShowPopup(location);
        }
        internal void ShowReminder(List<Task> reminders)
        {
            bool allowReminders = reminders != null && reminders.Count > 0;
            bbiReminder.Visibility = allowReminders ? BarItemVisibility.Always : BarItemVisibility.Never;
            bsiTemp.Visibility = allowReminders ? BarItemVisibility.Never : BarItemVisibility.Always;
            if (allowReminders)
            {
                bbiReminder.Caption = string.Format(Properties.Resources.ReminderText, reminders.Count);
                bbiReminder.Tag = reminders;
            }
        }
        internal void ShowInfo(int? count)
        {
            if (count == null) bsiInfo.Caption = string.Empty;
            else
                bsiInfo.Caption = string.Format(Properties.Resources.InfoText, count.Value);
            HtmlText = string.Format("{0}{1}", GetModuleName(), GetModulePartName());
            HtmlText = "汇创H6数据采集平台";
        }
        string GetModuleName()
        {
            if (string.IsNullOrEmpty(modulesNavigator.CurrentModule.PartName)) return CurrentModuleName;
            return string.Format("<b>{0}</b>", CurrentModuleName);
        }
        string GetModulePartName()
        {
            if (string.IsNullOrEmpty(modulesNavigator.CurrentModule.PartName)) return null;
            return string.Format(" - {0}", modulesNavigator.CurrentModule.PartName);

        }
        private void navBarControl1_ActiveGroupChanged(object sender, DevExpress.XtraNavBar.NavBarGroupEventArgs e)
        { 
            object data = GetModuleData((NavBarGroupTagObject)e.Group.Tag);
            modulesNavigator.ChangeGroup(e.Group, data);
        }
        private void bbi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag));
        }

        private void ucMailTree1_DataSourceChanged(object sender, DataSourceChangedEventArgs e)
        {
            currentMailType = e.Type;
            //modulesNavigator.CurrentModule.MessagesDataChanged(e);
            //ShowInfo(e.List.Count);
        }
        private void ucMailTree1_ShowMenu(object sender, MouseEventArgs e)
        {
            //pmTreeView.ShowPopup(ucMailTree1.PointToScreen(e.Location));
        }
        private void pmTreeView_BeforePopup(object sender, CancelEventArgs e)
        {
            bciShowAllMessageCount.Checked = DataHelper.ShowAllMessageCount;
            bciShowUnreadMessageCount.Checked = DataHelper.ShowUnreadMessageCount;
            //biCreateFolder.Enabled = !ucMailTree1.IsDeletedFolderFocused();
            //biRename.Enabled = !ucMailTree1.IsDeletedFolderFocused();
        }
        private void bciShowAllMessageCount_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataHelper.ShowAllMessageCount = bciShowAllMessageCount.Checked;
            //ucMailTree1.RefreshTreeList();
        }
        private void bciShowUnreadMessageCount_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataHelper.ShowUnreadMessageCount = bciShowUnreadMessageCount.Checked;
            //ucMailTree1.RefreshTreeList();
        }
        private void frmMain_KeyDown(object sender, KeyEventArgs e)
        {
            if (modulesNavigator != null && modulesNavigator.CurrentModule != null)
                modulesNavigator.CurrentModule.SendKeyDown(e);
        }
        protected object GetModuleData(NavBarGroupTagObject tag)
        {
            if (tag == null) return null;
            //if (tag.ModuleType == typeof(SmartFrame_DataCollect.Calendar)) return ucCalendar1;
            //if(tag.ModuleType == typeof(SmartFrame_DataCollect.Feeds)) return navBarControl2;
            //   if(tag.ModuleType == typeof(SmartFrame_DataCollect.Tasks)) return nbgTasks;
            return null;
        }
        private void navBarControl1_NavPaneStateChanged(object sender, EventArgs e)
        {
            //ucCalendar1.StateChanged(navBarControl1.OptionsNavPane.ActualNavPaneState);
            SetPageLayoutStyle();
        }
        object moduleTag = null;
        private void ucContacts1_CheckedChanged(object sender, EventArgs e)
        {
            moduleTag = ((CheckEdit)sender).Tag;
            BeginInvoke(new MethodInvoker(NavigateModule));
        }
        void NavigateModule()
        {
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", moduleTag));
        }

        private void bvbiExit_ItemClick(object sender, BackstageViewItemEventArgs e)
        {
            this.Close();
        }

        private void galleryControlGallery1_ItemClick(object sender, GalleryItemClickEventArgs e)
        {
            if (TagResources.OpenCalendar.Equals(e.Item.Tag))
            {
                ribbonControl1.HideApplicationButtonContentControl();
                this.Refresh();
                navBarControl1.ActiveGroup = nbgCalendar;
            }
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag)

                );
        }

        private void backstageViewControl1_ItemClick(object sender, BackstageViewItemEventArgs e)
        {
            if (modulesNavigator.CurrentModule == null) return;
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag));
        }
        void SetPageLayoutStyle()
        {
            bbiNormal.Down = navBarControl1.OptionsNavPane.NavPaneState == NavPaneState.Expanded;
            bbiReading.Down = navBarControl1.OptionsNavPane.NavPaneState == NavPaneState.Collapsed;
        }

        private void bbiNormal_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (bbiNormal.Down) navBarControl1.OptionsNavPane.NavPaneState = NavPaneState.Expanded;
            else
                bbiNormal.Down = true;
        }

        private void bbiReading_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (bbiReading.Down) navBarControl1.OptionsNavPane.NavPaneState = NavPaneState.Collapsed;
            else
                bbiReading.Down = true;
        }

        private void rgbiCurrentView_GalleryInitDropDownGallery(object sender, InplaceGalleryEventArgs e)
        {
            e.PopupGallery.GalleryDropDown.ItemLinks.Add(bbiManageView);
            e.PopupGallery.GalleryDropDown.ItemLinks.Add(bbiSaveCurrentView);
            e.PopupGallery.SynchWithInRibbonGallery = true;
        }
        
        private void rgbiCurrentViewTasks_GalleryItemClick(object sender, GalleryItemClickEventArgs e)
        {
            modulesNavigator.CurrentModule.ButtonClick(string.Format("{0}", e.Item.Tag));
        }

        private void ucCalendar1_VisibleChanged(object sender, EventArgs e)
        {
            //if (ucCalendar1.Visible)
            //    ucCalendar1.UpdateTreeListHeight();
        }

        private void bvtiPrint_SelectedChanged(object sender, BackstageViewItemEventArgs e)
        {
            //if(
            //backstageViewControl1.SelectedTab == bvtiPrint)
            //    this.printControl1.InitPrintingSystem();
        }
        private void ribbonControl1_BeforeApplicationButtonContentControlShow(object sender, EventArgs e)
        {
            //if(backstageViewControl1.SelectedTab == bvtiPrint) backstageViewControl1.SelectedTab = bvtiInfo;
            //bvtiPrint.Enabled = CurrentRichEdit != null || CurrentPrintableComponent != null;
            //bvtiExport.Enabled = CurrentExportComponent != null;
        }
        public IPrintable CurrentPrintableComponent { get { return modulesNavigator.CurrentModule.PrintableComponent; } }
        public IPrintable CurrentExportComponent { get { return modulesNavigator.CurrentModule.ExportComponent; } }
        public RichEditControl CurrentRichEdit { get { return modulesNavigator.CurrentModule.CurrentRichEdit; } }
        public string CurrentModuleName { get { return modulesNavigator.CurrentModule.ModuleName; } }

        private void ribbonControl1_ShowCustomizationMenu(object sender, RibbonCustomizationMenuEventArgs e)
        {
            e.CustomizationMenu.InitializeMenu();
            if (e.Link == null || !AllowCustomizationMenuList.Contains(e.Link.Item))
                e.CustomizationMenu.RemoveLink(e.CustomizationMenu.ItemLinks[0]);
        }

        private void ucMailTree1_UCTreeDragDrop(object sender, UCTreeDragDropEventArgs e)
        {
            //if (modulesNavigator.CurrentModule is Mail)
            //{
            //    ((Mail)modulesNavigator.CurrentModule).OnMoveEmails(ucMailTree1, e);
            //}
        }

        void biRename_ItemClick(object sender, ItemClickEventArgs e)
        {
            //ucMailTree1.StartEditing();
        }

        void biCreateFolder_ItemClick(object sender, ItemClickEventArgs e)
        {
            //ucMailTree1.CreateNewFolder();
        }

        private void bbiReminder_ItemClick(object sender, ItemClickEventArgs e)
        {
            using (frmReminders frm = new frmReminders())
            {
                frm.InitData(bbiReminder.Tag as List<Task>);
                if (frm.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    modulesNavigator.CurrentModule.FocusObject(frm.CurrentTask);
                    modulesNavigator.CurrentModule.ButtonClick(TagResources.TaskEdit);
                }
            }
        }

        private void barStaticItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            frm.Show();
        }


        public void hideLogin(string userDbName,string userName)
        {
            this.barStaticItem1.Caption = userName;
            this.barStaticItem1.Enabled = false;
            this.barSubItem4.Visibility = BarItemVisibility.Always;

            //刷新列数据
            treeList1.KeyFieldName = "MainID";
            treeList1.ParentFieldName = "ParentID";
            //treeList1.CustomDrawNodeCell += TreeList1_CustomDrawNodeCell;
            treeList1.FocusedNodeChanged += TreeList1_FocusedNodeChanged;
            treeList1.DataSource = SelectTreeData(userDbName,0);
            treeList1.ExpandAll();
            ////异步线程
            //Thread tr = new Thread(() =>
            //{
            //    //生成控件树节点,进入对应的线程，重新变为同步
            //    this.ucMailTree1.Invoke(new Action(() =>
            //    {
            //        //this.ucMailTree1.defaultTreeData("正在加载数据......");
            //    }));
            //    //变回异步
                
            //    //继续同步
            //    this.ucMailTree1.Invoke(new Action(() =>
            //    {
                    
            //    }));
       
            //});
            //tr.IsBackground = true;
            //tr.Start();
        }

        private void TreeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            string type = e.Node.GetValue("Type").ToString();
            string equipId = e.Node.GetValue("MainID").ToString();
            if(type == "1")
            {
                var deviceDetail = new SmartFrame_DataCollect.DeviceDetails();
                modulesNavigator.ShowSingleGroup(deviceDetail);
                deviceDetail.curEuipID = equipId;
                deviceDetail.InitControlConfig();
            }
        }

        //private void TreeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
        //{
        //    string textColor = GetHtmlTextColor(treeList1.FocusedNode.Equals(e.Node));
        //    object textValue = e.Node.GetValue(e.Column.Name);
        //    e.CellText = string.Format("<Color={1}>{0}", textValue, textColor);
        //    if (e.Node.ParentNode == null || !(DataHelper.ShowAllMessageCount || DataHelper.ShowUnreadMessageCount)) return;
        //    e.CellText = string.Format("<Color={2}>{0}<Size=-1><Color={1}>", textValue, ColorHelper.HtmlWarningColor, textColor);

        //}

        static string GetHtmlTextColor(bool focused)
        {
            if (focused)
                return ColorHelper.HtmlHighlightTextColor;
            return ColorHelper.HtmlControlTextColor;
        }

        public List<CollectDeviceMenu> SelectTreeData(string userDbName, int parentID)
        {
            List<CollectDeviceMenu> listCollectMenu = new List<CollectDeviceMenu>();
            string SqlString = "SELECT baetype.EquipmentTypeID, baetype.EquipmentTypeName FROM " + userDbName + ".dbo.Ba_EquipmentType as baetype LEFT JOIN " + userDbName + ".dbo.Ba_Equipment as bae ON baetype.EquipmentTypeID = bae.UpID WHERE IsCJ = 1 GROUP BY EquipmentTypeName, baetype.EquipmentTypeID";
            DataRowCollection rows = CommonSql._Query(SqlString, new Dictionary<string, object>()).Tables[0].Rows;
            foreach (DataRow item in rows)
            {
                listCollectMenu.Add(new CollectDeviceMenu()
                {
                    MainID = int.Parse(item["EquipmentTypeID"].ToString()+1000000),
                    ParentID = 0,
                    Name = item["EquipmentTypeName"].ToString(),
                    Type = 0
                });
            }

            SqlString = "SELECT baetype.EquipmentTypeID,EquipmentCode,EquipmentName,EquipmentID FROM " + userDbName + ".dbo.Ba_EquipmentType as baetype LEFT JOIN " + userDbName + ".dbo.Ba_Equipment as bae ON baetype.EquipmentTypeID = bae.UpID WHERE IsCJ = 1 ";

            DataRowCollection rows2 = CommonSql._Query(SqlString, new Dictionary<string, object>()).Tables[0].Rows;
            foreach (DataRow item2 in rows2)
            {
                listCollectMenu.Add(new CollectDeviceMenu()
                {
                    MainID = int.Parse(item2["EquipmentID"].ToString()),
                    ParentID = int.Parse(item2["EquipmentTypeID"].ToString() + 1000000),
                    Name = item2["EquipmentName"].ToString()+"【"+ item2["EquipmentCode"] +"】",
                    Type = 1
                });
            }
            return listCollectMenu;
        }




        private void backstageViewControl1_Click(object sender, EventArgs e)
        {
            
        }

        private void ucMailTree1_Load(object sender, EventArgs e)
        { 

        }

        private void bbiFlipLayout_ItemClick(object sender, ItemClickEventArgs e)
        {
            
        }

      

        private void barButtonItem8_ItemClick(object sender, ItemClickEventArgs e)
        {
            this.barStaticItem1.Caption = "登录";
            this.barStaticItem1.Enabled = true;
            this.barSubItem4.Visibility = BarItemVisibility.Never;
        }

        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
        {
            //object data = GetModuleData(new NavBarGroupTagObject("Tasks", typeof(SmartFrame_DataCollect.Tasks)));
            //modulesNavigator.ChangeGroup(navBarControl1.ActiveGroup, data);
            modulesNavigator.ShowSingleGroup(new SmartFrame_DataCollect.Tasks());
        }

        //private void BarButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        //{
        //    modulesNavigator.ShowSingleGroup(new SmartFrame_DataCollect.Tasks());
        //}

        private void BblLog_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            modulesNavigator.ShowSingleGroup(new SmartFrame_DataCollect.LogView());
        }

        private void DB_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //e.Item.Caption
            var dataConfigTab = new SmartFrame_DataCollect.DataConfigTab();
            modulesNavigator.ShowSingleGroup(dataConfigTab);
            dataConfigTab.ChangeDataConfig(e.Item.Caption);
        }
    }
}
